var express = require("express");
var uunewid = require("uuid");
var router = express.Router();
const { ohana } = require("ohana-node-orm");
let log = require("cf-nodejs-logging-support");
log.setLoggingLevel("info");

//创建文件表答案
router.post("/", async (req, res, next) => {
    // #swagger.tags = ['文件表']
    // #swagger.summary = '文件列表'
    /* #swagger.security = [{
                   "JiangduJWT": []
      }] */
    /*	#swagger.requestBody = {
                required: true,
                content: {
                    "application/json": {
                        schema: {
                            $ref: "#/definitions/questionnaire"
                        }  
                    },
                }
        } */

    /***
     * insertJsonDoc
     *   {
     *  ID:"五大服务的ID"
     *  detail：[] fileList
     * }
     *
     */
    const { data } = req.body;
    const result = await insertJsonDoc(data);
    if (result == 1) {
        res.sendStatus(200);
    } else {
        res.sendStatus(500);
    }
});

//list文件表答案
/**
 * "USER_ID": "455bdad0-4cbb-4bc4-85b0-0c34c0e4d8c4"
 * 文件表id
 **/
router.get("/", async (req, res, next) => {
    // #swagger.tags = ['文件表']
    // #swagger.summary = '获得所有的文件list'
    /* #swagger.security = [{
                   "JiangduJWT": []
      }] */
    /* #swagger.parameters['ID'] = {
            description: 'ID',
            required: true,
            type: 'string',
    } */
    const queryData = req.query;
    const result = await getJsonDoc(queryData);
    res.send(result);
});

//更新文件表 只能更新Answers
/**
{
    "data":{"ANSWERS": {"TEST": "132", "COOL": "it works"}, "UUID": "455bdad0-4cbb-4bc4-85b0-0c34c0e4d8c4"}
}
**/
router.put("/", async (req, res, next) => {
    // #swagger.tags = ['文件表']
    // #swagger.summary = '更新文件表'
    /* #swagger.security = [{
                   "JiangduJWT": []
      }] */
    /*	#swagger.requestBody = {
                required: true,
                content: {
                    "application/json": {
                        schema: {
                            $ref: "#/definitions/questionnaire_update"
                        }  
                    },
                }
        } */
    const { data } = req.body;
    const deleteResult = await deleteJsonDoc(data);
    const result = await insertJsonDoc(data);
    if (result == 1) {
        res.sendStatus(200);
    } else {
        res.sendStatus(500);
    }
});

/*
 *删除文件表id
 */

router.delete("/", async (req, res, next) => {
    // #swagger.tags = ['文件表']
    // #swagger.summary = '删除文件表'
    /* #swagger.security = [{
                   "JiangduJWT": []
      }] */
    /*	#swagger.requestBody = {
              required: true,
              content: {
                  "application/json": {
                      schema: {
                          $ref: "#/definitions/questionnaire_delete"
                      }  
                  },
              }
      } */
    const { data } = req.body;
    const result = await deleteJsonDoc(data);

    if (result == 1) {
        res.sendStatus(200);
    } else {
        res.sendStatus(500);
    }
});

async function insertJsonDoc(data) {
    const histo = new ohana("SAP_JIANGDU_FREQUENTLY_USED_ENTERPRISES");
    var json = JSON.stringify(data); // {"type":"Fiat","model":"500","color":"White"}为了去掉"符合HANA json 插入的格式
    var unquoted = json.replace(/"([^"]+)":/g, "$1:");
    let resultText = unquoted.replace(new RegExp('"', "g"), "'");
    var stringText = "insert into FILELIST values(" + resultText + ")";
    const result = await histo.raw(stringText);
    return result;
}

async function getJsonDoc(data) {
    const histo = new ohana("SAP_JIANGDU_FREQUENTLY_USED_ENTERPRISES");
    var stringText = "select * from  FILELIST where ID = '" + data.ID + "'";
    const result = await histo.raw(stringText);
    if (result.length > 0) {
        return { data: result[0]["FILELIST"], code: "ok" };
    } else {
        return { message: "没有值", code: -1 };
    }
}

async function deleteJsonDoc(data) {
    const histo = new ohana("SAP_JIANGDU_FREQUENTLY_USED_ENTERPRISES");
    var stringText = "delete from  FILELIST where ID = '" + data.ID + "'";
    const result = await histo.raw(stringText);
    return result;
}

module.exports = router;
